package in.abip.chisame;

import java.util.List;

import in.abip.chisame.model.Door;
import in.abip.chisame.model.ISwitch;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

/**
 * Light activity.
 * 
 * @author Julot.
 * @since Thursday, January 26th, 2011.
 */
public class DoorListActivity extends NetworkActivity {
	private Adapter _adapter = null;
	
	/**
	 * Adapter for light list view.
	 * 
	 * @author Julot.
	 * @since Thursday, January 27, 2011.
	 */
	private class Adapter extends ListAdapter {
		public Adapter(Context context, int viewResourceId,
				List<Object> objects) {
			super(context, viewResourceId, objects);
		}
		
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = super.getView(position, convertView, parent);
			SeekBar seekBar = (SeekBar) view.findViewById(R.id.seek);
			seekBar.setOnSeekBarChangeListener(_changeListener);
			Door item = (Door) _objects.get(position);
			seekBar.setTag(item);
			seekBar.setProgress(item.getValue());
			//if (item.hasRange) seekBar.setVisibility(SeekBar.VISIBLE);
			//else seekBar.setVisibility(SeekBar.INVISIBLE);
			ImageView image = (ImageView) view.findViewById(R.id.image);
			image.setOnClickListener(_clickListener);
			TextView text = (TextView) view.findViewById(R.id.text);
			text.setOnClickListener(_clickListener);
			text = (TextView) view.findViewById(R.id.value);
			text.setText(item.getValueName());
			return view;
		}
	}
	
	/**
	 * Listener for image and text click.
	 */
	private OnClickListener _clickListener = new OnClickListener() {
		public void onClick(View view) {
			Door model = (Door) view.getTag();
			model.toggle();
			send(model.getRunCmd());
			_adapter.notifyDataSetChanged();
		}
	};
	
	/**
	 * Listener for seek bar.
	 */
	private OnSeekBarChangeListener _changeListener = 
			new OnSeekBarChangeListener() {
		public void onStopTrackingTouch(SeekBar seekBar) {
			Door model = (Door) seekBar.getTag();
			model.setValue(seekBar.getProgress());
			send(model.getRunCmd());
			_adapter.notifyDataSetChanged();
		}
		
		public void onStartTrackingTouch(SeekBar seekBar) {
		}
		
		public void onProgressChanged(
			SeekBar seekBar,
			int progress,
			boolean fromUser
		) {
			View parent = (View) seekBar.getParent();
			ISwitch model = (ISwitch) seekBar.getTag();
			TextView text = (TextView) parent.findViewById(R.id.value);
			text.setText(model.getValueName(seekBar.getProgress()));
		}
	};
	
	/** Called when the activity is first created. */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_activity);
	}
	
	/** Populate list view. */
	@Override
	protected void populate() {
		_adapter = new Adapter(
			this,
			R.layout.list_item_seekbar,
			loadModels()
		);
		ListView view = (ListView) findViewById(R.id.list);
		view.setAdapter(_adapter);
	}
	
	/**
	 * Load models.
	 * @return {@link List}
	 */
	private List<Object> loadModels() {
		Bundle bundle = getIntent().getExtras();
		List<Object> models = null;
		if (bundle != null) {
			models = Door.model().findAll(
				"roomId = " + bundle.getInt("roomId")
			);
		}
		else models = Door.model().findAll();
		return models;
	}
	
	/**
	 * Refresh list with current status from server.
	 */
	protected void refresh() {
		String command;
		for (Object item : _adapter.getItems()) {
			Door model = (Door) item;
			command = model.getAskCmd();
			udpOutputs.add(command);
			tcpOutputs.add(command);
		}
		startSend();
	}
	
	protected void updateUi() {
		_adapter.notifyDataSetChanged();
	};
	
}
